package com.ruoyi.wms.controller;

import java.util.Iterator;
import java.util.List;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.wms.domain.Tree;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.wms.domain.WmsItemGroups;
import com.ruoyi.wms.service.IWmsItemGroupsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;


/**
 * 物料分组管理Controller
 * 
 * @author ruoyi
 * @date 2021-10-13
 */
@RestController
@RequestMapping("/wms/wmsItemGroups")
public class WmsItemGroupsController extends BaseController
{
    @Autowired
    private IWmsItemGroupsService wmsItemGroupsService;
    /**
     * 列表 - 物料分组
     */
    @PreAuthorize(hasPermi="wms:wmsItemGroups:list")
    @GetMapping("/list")
    public AjaxResult list(WmsItemGroups wmsItemGroups) {
        List<WmsItemGroups> groups = wmsItemGroupsService.selectGroupList(wmsItemGroups);
        return AjaxResult.success(groups);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize(hasPermi="wms:wmsItemGroups:list")
    @GetMapping("/list/exclude/{groupId}")
    public AjaxResult excludeChild(@PathVariable(value = "groupId", required = false) Long groupId) {
        List<WmsItemGroups> groups = wmsItemGroupsService.selectGroupList(new WmsItemGroups());
        Iterator<WmsItemGroups> it = groups.iterator();
        while (it.hasNext()) {
            WmsItemGroups d = (WmsItemGroups) it.next();
            if (d.getGroupId().intValue() == groupId
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), groupId + "")) {
                it.remove();
            }
        }
        return AjaxResult.success(groups);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @PreAuthorize(hasPermi="wms:wmsItemGroups:query")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable Long groupId) {
        wmsItemGroupsService.checkGroupDataScope(groupId);
        return AjaxResult.success(wmsItemGroupsService.selectGroupById(groupId));
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(WmsItemGroups wmsItemGroups) {
        List<WmsItemGroups> groups = wmsItemGroupsService.selectGroupList(wmsItemGroups);
        List<Tree> lt = wmsItemGroupsService.buildGroupTreeSelect(groups);
        return AjaxResult.success(lt);
    }

    /**
     * 新增物料分组管理
     */
    @PreAuthorize(hasPermi = "wms:wmsItemGroups:add")
    @Log(title = "物料分组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsItemGroups wmsItemGroups)
    {
        return toAjax(wmsItemGroupsService.insertGroup(wmsItemGroups));
    }

    /**
     * 修改物料分组管理
     */
    @PreAuthorize(hasPermi = "wms:wmsItemGroups:edit")
    @Log(title = "物料分组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsItemGroups wmsItemGroups)
    {
        return toAjax(wmsItemGroupsService.updateGroup(wmsItemGroups));
    }

    /**
     * 删除物料分组管理
     */
    @PreAuthorize(hasPermi = "wms:wmsItemGroups:remove")
    @Log(title = "物料分组管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long groupIds)
    {
        return toAjax(wmsItemGroupsService.deleteGroupById(groupIds));
    }
}
